package io.github.harmonly.controller;

import io.github.harmonly.entity.User;
import io.github.harmonly.service.ApplyService;
import io.github.harmonly.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@Controller
public class UserPageController {

    @Resource
    private UserService service;
    @Resource
    private ApplyService applyService;

    @RequestMapping(value = "/center", method = RequestMethod.GET)
    public ModelAndView getCenter(HttpSession session, int uid) {
        User user = (User) session.getAttribute("user");
        if (user == null) return new ModelAndView("redirect:/login");
        if (user.getUid() != uid) return new ModelAndView("redirect:/index");
        ModelAndView modelAndView = new ModelAndView("user/center");
        modelAndView.getModel().put("user", service.getUser(uid));
        return modelAndView;
    }

    @RequestMapping(value = "/message", method = RequestMethod.GET)
    public ModelAndView getMessage(HttpSession session, int uid) {
        User user = (User) session.getAttribute("user");
        if (user == null) return new ModelAndView("redirect:/login");
        if (user.getUid() != uid) return new ModelAndView("redirect:/index");
        ModelAndView modelAndView = new ModelAndView("user/message");
        modelAndView.getModel().put("user", service.getUser(uid));
        modelAndView.getModel().put("applies", applyService.getAppliesBySender(uid));
        return modelAndView;
    }
}
